﻿@{
    ViewBag.Title = "采购单列表";
    Layout = "~/Views/Shared/_LayoutList.cshtml"; 
}
@(       
 Html.nbGridFor<MaterialPurchase>(s =>
 {
     s.Action("List");
     s.ExtParam(new { date = DateTime.Today.AddDays(1) });
     s.Columns(c =>
     {
         c.Bind(e => e.Id).IsKey();
         c.BindDateTime(e => e.Date).AsSearch();
         c.Bind(e => e.SupplierName).Width(80).Align(GridAlign.Left).AsQuery().AsSearch();
         c.Bind(e => e.TotalAmount).Width(60).Align(GridAlign.Right);
         c.Bind(e => e.ItemDesc).Width("auto").Align(GridAlign.Left).AsQuery().AsSearch();
         c.BindEnum(e => e.State, "PurchaseState").Width(40).AsSearch();
         c.Bind(e => e.UpdateUser).Width(60);
         c.BindDateTime(e => e.UpdateDate);
         c.BindTemplate(250)
            .Template(@<a href="@Url.Action("Export")?id=${Id}" target="_blank" class="imgbtn2"><span
                class="print">打印</span></a>)
            .Template(@<a href="javascript:DeliveryItem('${Id}','${State}','${SupplierName}')"
                class="imgbtn2"><span class="accept">订货</span></a>)
            .Template(@<a href="javascript:StorageItem('${Id}','${State}','${SupplierName}')"
                class="imgbtn2"><span class="accept">入库</span></a>)
            .Template(Url.GridRowEditLink("编辑采购单"))
            .Template(@<a href="javascript:DeleteItem('${Id}','${State}','${SupplierName}')"
                class="imgbtn2"><span class="Delete">删除</span></a>);

     });
     s.Buttons(b =>
     {
         b.BindAddButton();
         b.Bind(GridButtonType.Edit, "批量订货").OnClick("DeliveryBatchRecord");
         b.Bind(GridButtonType.Import, "导出Word").OnClick("ExportWord");
     });
 }))
<script type="text/javascript">
    function DeleteItem(id, state, name) {
        if (state != "Draft") {
            alert("订单状态已经不是草稿了，不能删除！");
        }
        else {
            DeleteRecord('@Url.Action("Delete")?id=' + id, name)
        }
    }
    function StorageItem(id, state, name) {
        if (state != "Delivery") {
            alert("订单状态不是订单，不能入库！");
        }
        else {
            OpenModalDialog('@Url.Action("Storage")?Id=' + id,
            {
                caption: "采购入库（" + name + "）",
                width: 610,
                height: 450,
                onclose: refreshGrid
            });
        }
    }
    function DeliveryItem(id, state, name) {
        if (state != "Draft") {
            alert("订单状态不是采购，不能下订单！");
        }
        else {
            ajaxTodo('@Url.Action("Delivery")', { id: id }, refreshGrid, "你确定要向供应商（" + name + "）订货吗？")
        }
    }
    function DeliveryBatchRecord() {
        var ids = $("table.flexigrid").getCheckedRows(); //选中行的主键
        if (ids.length <= 0)
            alert("请您选择要订货的记录！");
        else {
            var data = $("table.flexigrid").getSelectedRows();
            for (var rowIndex in data) {
                var row = data[rowIndex];
                if (row.State != "Draft") {
                    alert("只有所选记录的状态全部都是草稿才能下订单！");
                    return;
                }
            }

            if (confirm("你确定要订货选中的记录吗?")) {
                showLoadingMsg("正在订货...", { left: 300, top: 2 });
                ajaxTodo('@Url.Action("Delivery")', { id: ids.join(",") }, refreshGrid)
            }
        }
    }
    function ExportWord() {
        var ids = $("table.flexigrid").getCheckedRows(); //选中行的主键
        if (ids.length <= 0)
            alert("请您选择要订货的记录！");
        else {
            var data = $("table.flexigrid").getSelectedRows();
            window.open('@Url.Action("ExportWord")?id=' + ids.join(","));
        }
    }
</script>
